<?php


namespace app\common\model;

use think\facade\Db;
use think\facade\Env;
use think\facade\Request;
class Config extends Base
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = true;
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    /**
     * ajax验证配置组英文名称
     */
    public function ajaxConfigName ($config_name, $config_id = 0)
    {
        if (empty($config_name)) {
            return 1;
        }
        //组装where条件
        $where = array();
        $where[] = array('config_name', '=', $config_name);
        if ($config_id > 0) {
            $where[] = array('config_id', '<>', $config_id);
        }
        return Db::name('config')->where($where)->find();
    }

    /**
     * ajax验证配置组英文名称
     */
    public function ajaxConfigTitle ($config_title, $config_id = 0)
    {
        if (empty($config_title)) {
            return 1;
        }
        //组装where条件
        $where = array();
        $where[] = array('config_title', '=', $config_title);
        if ($config_id > 0) {
            $where[] = array('config_id', '<>', $config_id);
        }
        return Db::name('config')->where($where)->find();
    }

    public function saveData ($where = array(), $data = array())
    {
        Db::name('config')->where($where)->update($data);
        $this->saveConfig();
        return true;
    }

    /**
     * 动态写入配置文件
     */
    private  function  saveConfig(){
        //清楚缓存

        $page_data = Db::name('config')->where(array('config_status'=>1))->order('config_id asc')->select();
        $arr = array();
        if(!empty($page_data)){
            foreach($page_data as $k=>$v){
                $name = strtoupper($v['config_name']);
                if(strtoupper($v['config_value']) == "FALSE"){
                    $v['config_value'] = FALSE;
                }
                if(strtoupper($v['config_value']) == "TRUE"){
                    $v['config_value'] = true;
                }
                $arr[$name] = htmlspecialchars_decode($v['config_value']);

            }
        }
        //写入配置文件
        $content = "<?php return ".var_export($arr,true)."\n?>";
        return file_put_contents("../config/site.php",$content);


    }


    /**获取网站配置数据
     * @param $where
     */
    public function webConfig ($where)
    {
        $page_data = Db::name('config_group')->where($where)->order('group_sort asc')->select()->all();
        if (!empty($page_data)) {
            foreach ($page_data as $k => $v) {
                $where = array();
                $where['group_id'] = $v['group_id'];
                $where['config_status'] = 1;
                $config = Db::name('config')->where($where)->order('config_sort asc,config_id asc')->select()->all();
                if ($config) {
                    foreach ($config as $k1 => $v1) {
                        $func = "_" . $v1['config_type'];
                        $config[$k1]['html'] = $this->$func($v1);
                    }
                }
                $page_data[$k]['config'] = $config;
            }
        }
        return $page_data;
    }

    /**
     * 输入框
     */
    private function _text ($config)
    {
        return '<input type="text" name="config[' . $config['config_id'] . '][config_value]" value="' . $config['config_value'] . '" class="layui-input"/>';
    }

    /**
     * 单选框
     */
    private function _radio ($config)
    {
        $info = explode(",", $config['config_info']);
        $html = "";
        foreach ($info as $k => $radio) {
            $data = explode("|", $radio);//[0]值如1 [1]描述如开启
            $checked = $data[0] == $config['config_value'] ? 'checked="checked"' : '';

            $k = $k + 1;
            if ($k % 3 == 0) {
                $html.='<input type="radio" name="config[' . $config['config_id'] . '][config_value]" value="'.$data[0].'" title="'.$data[1].'" '.$checked.'>';

            }else{
                $html.='<input type="radio" name="config[' . $config['config_id'] . '][config_value]" value="'.$data[0].'" title="'.$data[1].'" '.$checked.'>';

            }
        }
        return $html;
    }
    /**
     * 文本域
     */
    private function _textarea ($config)
    {
        return '<textarea  name="config[' . $config['config_id'] . '][config_value]" class="layui-textarea">' . $config['config_value'] . '</textarea>';
    }
    /**
     * 单选框
     */
    private function _select ($config)
    {
        $info = explode(",", $config['config_info']);
        $html = '<select name="config[' . $config['config_id'] . '][config_value]"  class="col-xs-10">';
        foreach ($info as $k => $radio) {
            $data = explode("|", $radio);//[0]值如1 [1]描述如开启
            $selected = $data[0] == $config['config_value'] ? 'selected="selected"' : '';
            $html.=' <option value="'.$data[0].'" '.$selected.'>'.$data[1].'</option>';

        }
        return $html;
    }

    private function _file ($config)
    {
        $defaultImg = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1558099751645&di=053234c0b63e4689455d0a392f873f4a&imgtype=0&src=http%3A%2F%2Fimg.tukexw.com%2Fimg%2Fe40fb8b53441be9b.jpg';

        if (empty($config['config_value'])) $config['config_value'] = $defaultImg;
        return'<input type="hidden" name="config[' . $config['config_id'] . '][config_value]"  value="' . $config['config_value'] . '" id="avatar' . $config['config_id'] . '">
                                    <div class="layui-upload">
                                        <button type="button" data-id="'. $config['config_id'] . '" class="adBtn layui-btn layui-btn-primary" id="adBtn">
                                        <i class="icon icon-upload3"></i>点击上传</button>
                                        <div class="layui-upload-list">
                                            <img src="' . $config['config_value'] . '" class="layui-upload-img" id="adPic' . $config['config_id'] . '" width="30%">
                                            <p id="demoText"></p>
                                        </div>
                                    </div>';
    }



}
